/* $Id: ContestUsersModel.java 129 2011-08-26 05:27:43Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.models;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;

import alt.djudge.frontend.server.Deployment;
import alt.djudge.frontend.server.datatypes.ContestUserEntry;
import alt.djudge.frontend.server.datatypes.UserEntry;


public class ContestUsersModel
{
	private static CoreInterface<ContestUserEntry> core = Deployment.isGAE() ?  
		new AbstractBigtableModelCore<ContestUserEntry>()
		{
			@Override
			protected Order getOrdering()
			{
				return Order.asc("id");
			}
			
			@Override
			protected Class<ContestUserEntry> getEntryClass()
			{
				return ContestUserEntry.class;
			}
		}
	:
		new AbstractDBModelCore<ContestUserEntry>()
		{
			@Override
			protected Order getOrdering()
			{
				return Order.asc("id");
			}
			
			@Override
			protected Class<ContestUserEntry> getEntryClass()
			{
				return ContestUserEntry.class;
			}
		};		
	
	public static List<ContestUserEntry> getAllEntries()
	{
		return core.getAllEntries();
	}

	public static List<ContestUserEntry> getEntries(Map<String, Object> map)
	{
		return core.getEntries(map);
	}

	public static List<UserEntry> getContestEntries(Long contestId)
	{
		Map<Long, UserEntry> users = UsersModel.getEntriesMap();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contestId", contestId);
		map.put("active", true);
		List<ContestUserEntry> list = core.getEntries(map);
		List<UserEntry> result = new ArrayList<UserEntry>();
		for (ContestUserEntry ContestUserEntry : list)
			result.add(users.get(ContestUserEntry.getUserId()));
		return result;
	}
	
	public static List<UserEntry> getContestEntries(String contestId)
	{
		return getContestEntries(Long.parseLong(contestId));
	}

	public static List<ContestUserEntry> getContestUserEntries(String contestId)
	{
		return getContestUserEntries(Long.parseLong(contestId));
	}
	
	public static List<ContestUserEntry> getContestUserEntries(Long contestId)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contestId", contestId);
		map.put("active", true);
		List<ContestUserEntry> list = core.getEntries(map);
		return list;
	}

	public static ContestUserEntry getEntry(Long contestLanguageId)
	{
		return core.getEntry(contestLanguageId);
	}
	
	public static ContestUserEntry getOrCreateEntry(Long contestId, Long userId)
	{
		ContestUserEntry entry = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contestId", contestId);
		map.put("userId", userId);
		List<ContestUserEntry> list = core.getEntries(map);
		if (list.size() > 0)
		{
			entry = list.get(0);
		}
		else
		{
			UserEntry user = UsersModel.getUser(userId);
			entry = new ContestUserEntry(contestId, userId);
			entry.setName(user.getUsername());
			entry.setRole(user.getRole());
			entry.makePersistent();
		}
		return entry;
	}
	
	public static ContestUserEntry getEntry(Long contestId, Long userId)
	{
		ContestUserEntry entry = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contestId", contestId);
		map.put("userId", userId);
		List<ContestUserEntry> list = core.getEntries(map);
		if (list.size() > 0)
		{
			entry = list.get(0);
		}
		return entry;
	}
	
	public static boolean deleteEntry(Long contestProblemId)
	{
		return core.deleteEntry(contestProblemId);
	}
	
	public static CoreInterface<ContestUserEntry> getCore()
	{
		return core.getAdapter();
	}
}
